
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#define EXIT_SUCCES 0

extern void wc(int , int *, int *, int *, int *, int *, int *);

void imprimir_ayuda() {
        char *ayuda = "Usage:\n"
                        "\ttp1 -h\n"
                        "\ttp1 -V\n"
                        "\ttp1 [options] [file...]\n"
                        "Options:\n"
                        "-V, --version\n\tPrint version and quit.\n"
                        "-h, --help\n\tPrint this information and quit.\n"
                        "-w, --words\n\tPrint the number of words.\n"
                        "-l, --lines\n\tPrint the number of lines.\n"
                        "-c, --bytes\n\tPrint the number of bytes.\n";


        printf("%s", ayuda);
}


int main(int argc, char* argv[]) {

        int cont_palabra_total = 0;
        int cont_byte_total = 0;
        int cont_linea_total = 0;
        int cont_palabra = 0;
        int cont_byte = 0;
        int cont_linea = 0;
        FILE* f_input;

        // Imprimo la ayuda o la versión
        if ((argc == 2)
                        && ((strcmp(argv[1], "-h") == 0) || (strcmp(argv[1], "--help") == 0))) {
                imprimir_ayuda();
                return 0;
        } else if ((argc == 2)
                        && ((strcmp(argv[1], "-V") == 0)
                                        || (strcmp(argv[1], "--version") == 0))) {
                printf("tp1 for 6620-Orga de compus. 1ro C 2014 \n");
                return 0;
        }

       if ((argc == 2)
                       && ((strcmp(argv[1], "-w") == 0))) {
                       f_input = stdin;
                       wc(fileno(f_input), &cont_palabra_total, &cont_byte_total, &cont_linea_total, &cont_palabra, &cont_byte, &cont_linea);
                       printf("palabras: %d\n", cont_palabra);
                       fclose(f_input);
                       return 0;
        }


        if ((argc == 2)
        				&& ((strcmp(argv[1], "-l") == 0))) {
        				f_input = stdin;
                        wc(fileno(f_input), &cont_palabra_total, &cont_byte_total, &cont_linea_total, &cont_palabra, &cont_byte, &cont_linea);
                        printf("lineas: %d\n", cont_linea);
                        fclose(f_input);
                        return 0;
        }

        if ((argc == 2)
        				&& ((strcmp(argv[1], "-c") == 0))) {
        				f_input = stdin;
                        wc(fileno(f_input), &cont_palabra_total, &cont_byte_total, &cont_linea_total, &cont_palabra, &cont_byte, &cont_linea);
                        printf("bytes: %d\n", cont_byte);
                        fclose(f_input);
                        return 0;
        }

        if ((argc == 3)
                		&& (((strcmp(argv[1], "-c") == 0)) || (strcmp(argv[1], "-l") == 0))
                		&& ((strcmp(argv[2], "-c") == 0) || (strcmp(argv[2], "-l") == 0))) {
                		f_input = stdin;
                        wc(fileno(f_input), &cont_palabra_total, &cont_byte_total, &cont_linea_total, &cont_palabra, &cont_byte, &cont_linea);
                        printf("bytes: %d, lineas: %d\n", cont_byte, cont_linea);
                        fclose(f_input);
                        return 0;
        }

        if ((argc == 3)
                       && (((strcmp(argv[1], "-c") == 0)) || (strcmp(argv[1], "-w") == 0))
                       && ((strcmp(argv[2], "-c") == 0) || (strcmp(argv[2], "-w") == 0))) {
                       f_input = stdin;
                       wc(fileno(f_input), &cont_palabra_total, &cont_byte_total, &cont_linea_total, &cont_palabra, &cont_byte, &cont_linea);
                       printf("bytes: %d, palabras: %d\n", cont_byte, cont_palabra);
                       fclose(f_input);
                       return 0;
        }

        if ((argc == 3)
                      && (((strcmp(argv[1], "-w") == 0)) || (strcmp(argv[1], "-l") == 0))
                      && ((strcmp(argv[2], "-w") == 0) || (strcmp(argv[2], "-l") == 0))) {
                      f_input = stdin;
                      wc(fileno(f_input), &cont_palabra_total, &cont_byte_total, &cont_linea_total, &cont_palabra, &cont_byte, &cont_linea);
                      printf("palabras: %d, lineas: %d\n", cont_palabra, cont_linea);
                      fclose(f_input);
                      return 0;
        }

        int tam = 1;
        if (argc >= 2)
                tam = argc - 1;

        // Lista de los archivos a procesar
        FILE *lista_archivos[tam];

        // Si no hay argumentos considero stdin como el único archivo
        if (argc == 1) {
                lista_archivos[0] = stdin;
                wc(fileno(lista_archivos[0]), &cont_palabra_total, &cont_byte_total, &cont_linea_total, &cont_palabra, &cont_byte, &cont_linea);
                printf("palabras: %d, bytes: %d, lineas: %d\n", cont_palabra, cont_byte, cont_linea);
                fclose(lista_archivos[0]);
        } else {

			if (((strcmp(argv[1], "-w") == 0) || (strcmp(argv[1], "-l") == 0))
					&& (((strcmp(argv[2], "-w") == 0) || (strcmp(argv[2], "-l") == 0)))) {
				int j;
				for (j = 3; j <= tam; j++) {
					f_input = fopen(argv[j], "r");
					wc(fileno(f_input), &cont_palabra_total, &cont_byte_total, &cont_linea_total, &cont_palabra, &cont_byte, &cont_linea);
					printf("palabras: %d, lineas: %d, %s\n", cont_palabra, cont_linea, argv[j]);
					fclose(f_input);
				}
				if (tam > 3) {
					printf("Total: palabras: %d, lineas: %d\n", cont_palabra_total, cont_linea_total);
				}
				return 0;
			}

			if (((strcmp(argv[1], "-w") == 0) || (strcmp(argv[1], "-c") == 0))
					&& (((strcmp(argv[2], "-w") == 0) || (strcmp(argv[2], "-c") == 0)))) {
				int j;
				for (j = 3; j <= tam; j++) {
					f_input = fopen(argv[j], "r");
					wc(fileno(f_input), &cont_palabra_total, &cont_byte_total, &cont_linea_total, &cont_palabra, &cont_byte, &cont_linea);
					printf("palabras: %d, bytes: %d, %s\n", cont_palabra, cont_byte, argv[j]);
					fclose(f_input);
				}
				if (tam > 3) {
					printf("Total: palabras: %d, bytes: %d\n", cont_palabra_total, cont_byte_total);
				}
				return 0;
			}

			if (((strcmp(argv[1], "-l") == 0) || (strcmp(argv[1], "-c") == 0))
					&& (((strcmp(argv[2], "-l") == 0) || (strcmp(argv[2], "-c") == 0)))) {
				int j;
				for (j = 3; j <= tam; j++) {
					f_input = fopen(argv[j], "r");
					wc(fileno(f_input), &cont_palabra_total, &cont_byte_total, &cont_linea_total, &cont_palabra, &cont_byte, &cont_linea);
					printf("bytes: %d, lineas: %d, %s\n", cont_byte, cont_linea, argv[j]);
					fclose(f_input);
				}
				if (tam > 3) {
					printf("Total: bytes: %d, lineas: %d\n", cont_byte_total, cont_linea_total);
				}
				return 0;
			}

        	if((strcmp(argv[1], "-w") == 0)) {
        		int j;
        	    for (j = 2; j <= tam; j++){
        	       	f_input = fopen(argv[j], "r");
        	       	wc(fileno(f_input), &cont_palabra_total, &cont_byte_total, &cont_linea_total, &cont_palabra, &cont_byte, &cont_linea);
        	       	printf("palabras: %d, %s\n", cont_palabra, argv[j]);
        	       	fclose(f_input);
        	    }
        	    if (tam > 2){
        	    	printf("Total: palabras: %d\n", cont_palabra_total);
        	    }
        	    return 0;
            }

        	if ((strcmp(argv[1], "-l") == 0)) {
        		int j;
        		for (j = 2; j <= tam; j++) {
        			f_input = fopen(argv[j], "r");
           			wc(fileno(f_input), &cont_palabra_total, &cont_byte_total, &cont_linea_total, &cont_palabra, &cont_byte, &cont_linea);
        			printf("lineas: %d, %s\n", cont_linea, argv[j]);
        			fclose(f_input);
        		}
        		if (tam > 2) {
        			printf("Total: lineas: %d\n", cont_linea_total);
        		}
        		return 0;
        	}
			if ((strcmp(argv[1], "-c") == 0)) {
				int j;
				for (j = 2; j <= tam; j++) {
					f_input = fopen(argv[j], "r");
					wc(fileno(f_input), &cont_palabra_total, &cont_byte_total, &cont_linea_total, &cont_palabra, &cont_byte, &cont_linea);
					printf("bytes: %d, %s\n", cont_byte, argv[j]);
					fclose(f_input);
				}
				if (tam > 2) {
					printf("Total: bytes: %d\n", cont_byte_total);
				}
				return 0;
			}

       		int i;
       		// Recorro los archivos para abrir y procesarlos
            for (i = 0; i < tam; i++) {
            	cont_palabra = 0;
                cont_linea = 0;
				cont_byte = 0;
                lista_archivos[i] = fopen(argv[i + 1], "r");
                if (lista_archivos[i] == NULL) {
                	fprintf(stderr, "Imposible abrir %s\n", argv[i + 1]);
                    	return 1;
                }
                wc(fileno(lista_archivos[i]), &cont_palabra_total, &cont_byte_total, &cont_linea_total, &cont_palabra, &cont_byte, &cont_linea);
                printf("palabras: %d, bytes: %d, lineas: %d, %s\n", cont_palabra, cont_byte, cont_linea, argv[i + 1]);
                fclose(lista_archivos[i]);

            }

            if (tam > 1){
            	printf("Total: palabras: %d, bytes: %d, lineas: %d\n", cont_palabra_total, cont_byte_total,cont_linea_total);
            }

        }

        return EXIT_SUCCES;

}


